package com.chai.service.impl;

import com.chai.controller.Code;
import com.chai.dao.BookDao;
import com.chai.domain.Book;
import com.chai.exception.BusinessException;
import com.chai.exception.SystemException;
import com.chai.service.BookSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookServiceImpl implements BookSerivce {
    @Autowired
    private BookDao bookDao;
    /**
     * 增加
     *
     * @param book
     */
    @Override
    public boolean save(Book book) {
        int save = bookDao.save(book);
        System.out.println("影响的行数"+save);
        return save>0;
    }

    /**
     * 根据id进行删除数据
     *
     * @param id
     */
    @Override
    public boolean deleteById(int id) {
        //返回影响的行数
        int i = bookDao.deleteById(id);
        return i>0;
    }

    /**
     * 修改数据
     *
     * @param book
     */
    @Override
    public boolean update(Book book) {
        int update = bookDao.update(book);
        return update>0;
    }

    /**
     * 根据id进行查询
     *
     * @param id
     */
    @Override
    public Book selectById(int id) {
        /**模拟业务异常的处理*/
        if (id<0){
            throw new BusinessException("请输入正确的规范信息",Code.BUSINESS_ERR);
        }
       /* *//**模拟系统异常的处理*//*
        try{
            int i=1/0;
        }catch (Exception e){
            throw new SystemException("服务需要等待...",Code.SYSTEM_ERR);
        }*/
        Book book = bookDao.selectById(id);
        return book;
    }

    /**
     * 查询所有的数据
     */
    @Override
    public List<Book> selectAll() {
        List<Book> books = bookDao.selectAll();
        return books;
    }
}
